package com.bitdf.txing.common.config;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author Lizhiwei
 * @date 2023/8/30 18:41:03
 * 注释： TODO 待修改 createBy 和 updateBy 的类型
 */
@Component
@Slf4j
public class MyMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        String createBy = null;
        try {
            if (StpUtil.isLogin()) {
                long l = StpUtil.getLoginIdAsLong();
                createBy = String.valueOf(l);
            }
        } catch (Exception e) {
            createBy = "-1";
            log.info("系统保存");
        }
        this.strictInsertFill(metaObject, "createBy", String.class, createBy);
        this.strictInsertFill(metaObject, "createTime", Date.class, new Date());
        this.strictInsertFill(metaObject, "updateBy", String.class, createBy);
        this.strictInsertFill(metaObject, "updateTime", Date.class, new Date());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        String updateBy = null;
        try {
            if (StpUtil.isLogin()) {
                long l = StpUtil.getLoginIdAsLong();
                updateBy = String.valueOf(l);
            }
        } catch (Exception e) {
            updateBy = "-1";
            log.info("系统更新");
        }
        this.strictUpdateFill(metaObject, "updateBy", String.class, updateBy);
        this.strictUpdateFill(metaObject, "updateTime", Date.class, new Date());
    }
}
